class UsersController < ApplicationController


	before_action :require_user,except: [:index,:upload_callback]

	def index
		@users_count=User.count
		@users=User.all
	end
	def destroy

	end
	
	def show
		@topics=@user.topics.select("title, likes_count, replies_count,id,node_id").much_talked.limit(20)
		@replies=@user.replies.limit(10)
	end


	def topics
		@topics=@user.topics.select("title, likes_count, replies_count,id,node_id,created_at")
			        .paginate(page: params[:page],per_page: 20)
	end

	def replies
		@replies=@user.replies
	end

	def followers
		@users=User.where(id: @user.follower_ids )

	end

	def following
		@users=User.where(id: @user.following_ids )
	end

	def notes

	end



	def follow
		current_user.follow_user(@user)
		render json: {code:0,data:{followers_count: @user.followers_count} }
	end
	def unfollow
		current_user.unfollow_user(@user)
		render json: {code: 0,data:{followers_count: @user.followers_count} }
	end


		# @qiniu_upload_token=generate_qiniu_upload_token
	def upload_callback
		upload_ret= JSON.parse(Base64.urlsafe_decode64(params[:upload_ret]))
		current_user.update_attribute(:avatar,upload_ret['newname'])
			# title: upload_ret['title'],
			# filename: upload_ret['fname'],
			# qiniu_hash: upload_ret['hash']
		redirect_to :back
	end


	private
		# 找到用户
		def require_user
			@user= User.find(params[:id])
			if @user.nil?

			end
		end
end
